<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

class Index extends AdminBase
{
    public function index()
    {
        return $this->fetch();
    }
    public function welcome()
    {
        /*获取一些统计信息*/
        $customer_model = model('Customer');
        $customer_counts = $customer_model->getList([], 1, true);

        $contract_model = model('Contract');
        $contract_counts = $contract_model->getList([], 1, true);

        $admin_user_model = model('AdminUser');
        $admin_user_counts = $admin_user_model->getList([], 1, true);

        $version = Db::query('SELECT VERSION() AS ver');
        $config  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];

        /*按签订公司统计客户数量*/
        $sql = 'SELECT star_level,COUNT(*) as c FROM `ct_customer` WHERE is_delete = 0 GROUP BY sign_company_code';
        $count_customer = $customer_model->query($sql);
        $count_customer_arr = [];
        foreach ($count_customer as $v) {
            $tmp = [
                'name' => $v['star_level']. '星',
                'value' => $v['c'],
            ];
            $count_customer_arr[] = $tmp;
        }

        /*按签订公司统计合同数量*/
        $sql = 'SELECT sign_company_code,COUNT(*) as c FROM `ct_contract` WHERE is_delete = 0 GROUP BY sign_company_code';
        $count_contract = $customer_model->query($sql);
        $count_contract_arr = [];
        foreach ($count_contract as $v) {
            $tmp = [
                'name' => $v['sign_company_code'],
                'value' => $v['c'],
            ];
            $count_contract_arr[] = $tmp;
        }


        return $this->fetch('welcome', [
            'customer_counts' => $customer_counts,
            'contract_counts' => $contract_counts,
            'admin_user_counts' => $admin_user_counts,
            'config' => $config,
            'count_customer_json' => json_encode($count_customer_arr, JSON_UNESCAPED_UNICODE),
            'count_contract_json' => json_encode($count_contract_arr, JSON_UNESCAPED_UNICODE),
        ]);
    }
}
